package com.guangyunl.a_array;
// 索引越界异常 java.lang.ArrayIndexOutOfBoundsException
public class Demo09ArrayException {
    public static void main(String[] args) {
        // 定义长度为3的数组arr1
        int[] arr1 = new int[3];
        arr1[0] = 100;
        arr1[1] = 200;
        arr1[2] = 300;
        // arr1[3] = 400; // 出现异常，因为索引3超出了数组arr1[]的索引范围
        // arr1[-1] = 400; // 出现异常，因为索引-1超出了数组arr1[]的索引范围

        for (int i = 0; i <= 3; i++) {
            System.out.println("arr1[" + i + "] = " + arr1[i]);
        }
    }
}

/*
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: Index 3 out of bounds for length 3
	at com.guangyunl.a_array.Demo09Array.main(Demo09Array.java:10)
 */

/*
arr1[0] = 100
arr1[1] = 200
arr1[2] = 300
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: Index 3 out of bounds for length 3
	at com.guangyunl.a_array.Demo09ArrayException.main(Demo09ArrayException.java:14)
 */